Skip to content

Make viscosity inplace and avoid duplicate velocity loads#1125

Merged
svchb merged 17 commits intotrixi-framework:mainfrom
efaulhaber:inplace-viscosity
Apr 13, 2026
Merged

Make viscosity inplace and avoid duplicate velocity loads#1125
svchb merged 17 commits intotrixi-framework:mainfrom
efaulhaber:inplace-viscosity

Conversation

@efaulhaber
Copy link
Copy Markdown
Member

@efaulhaber efaulhaber commented Apr 1, 2026

This PR is part of #1131 and

  • makes the viscosity functions work inplace on a Ref value, which allows us to accumulate dv_viscosity over all neighbors before writing it to dv (with Improve GPU performance of fluid-* interact #1116),
  • makes viscous_velocity no-ops when the regular velocity is used, which we already have to load for the continuity equation,
  • moves some computations into the if-condition for Monaghan viscosity.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 94.32624% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.85%. Comparing base (ca35699) to head (d58afa4).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/schemes/boundary/wall_boundary/system.jl 60.00% 2 Missing ⚠️
src/schemes/structure/rigid_body/system.jl 0.00% 2 Missing ⚠️
...c/schemes/structure/total_lagrangian_sph/system.jl 0.00% 2 Missing ⚠️
src/schemes/fluid/viscosity.jl 98.64% 1 Missing ⚠️
...chemes/structure/total_lagrangian_sph/viscosity.jl 96.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1125       +/-   ##
===========================================
+ Coverage   67.03%   88.85%   +21.82%     
===========================================
  Files         128      128               
  Lines        9808     9864       +56     
===========================================
+ Hits         6575     8765     +2190     
+ Misses       3233     1099     -2134     
Flag Coverage Δ
total 88.85% <94.32%> (?)
unit 67.03% <68.08%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@efaulhaber efaulhaber mentioned this pull request Apr 7, 2026
7 tasks
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors viscosity evaluation to support in-place accumulation and reduce redundant velocity loads in SPH interaction kernels (targeting GPU performance improvements as part of #1131).

Changes:

  • Replaces return-by-value viscosity contributions with in-place accumulation via dv_viscosity!(dv_particle_ref, ...).
  • Preloads v_a/v_b and threads them through viscosity/viscous_velocity APIs to avoid duplicate current_velocity loads.
  • Updates tests and removes an unused test import.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/schemes/fluid/viscosity.jl Introduces dv_viscosity! in-place API; updates all viscosity models to accumulate into a Ref; adds viscous_velocity fast-path using preloaded velocities.
src/schemes/fluid/weakly_compressible_sph/rhs.jl Preloads velocities and switches viscosity term computation to dv_viscosity!.
src/schemes/fluid/implicit_incompressible_sph/rhs.jl Preloads velocities and switches viscosity term computation to dv_viscosity!.
src/schemes/fluid/implicit_incompressible_sph/system.jl Updates predicted-velocity step to use dv_viscosity! (in-place) and dereference via dv_viscosity_[].
src/schemes/fluid/entropically_damped_sph/rhs.jl Preloads velocities and switches viscosity term computation to dv_viscosity!.
src/schemes/boundary/open_boundary/rhs.jl Switches boundary viscosity handling to dv_viscosity! and uses preloaded velocities.
src/schemes/boundary/open_boundary/system.jl Avoids duplicate velocity loads by passing preloaded velocity into viscous_velocity.
src/general/interpolation.jl Avoids duplicate velocity loads by passing preloaded velocity into viscous_velocity.
src/schemes/boundary/wall_boundary/system.jl Updates viscous_velocity signatures to accept preloaded v_particle and become no-op when regular velocity is used.
src/schemes/structure/rigid_body/system.jl Updates viscous_velocity signature to accept preloaded velocity.
src/schemes/structure/total_lagrangian_sph/system.jl Updates viscous_velocity signature to accept preloaded velocity.
src/schemes/structure/structure.jl Preloads velocities and switches structure–fluid viscosity term computation to dv_viscosity!.
test/schemes/fluid/viscosity.jl Updates viscosity tests to use in-place API and dereference Ref results.
test/general/density_calculator.jl Removes unused OrdinaryDiffEq import.
Comments suppressed due to low confidence (3)

src/schemes/fluid/viscosity.jl:253

  • smoothing_length_neighbor is taken from particle_system, but neighbor is part of neighbor_system. For mixed-system interactions this will compute smoothing_length_average incorrectly. Use smoothing_length(neighbor_system, neighbor) for the neighbor smoothing length.

    smoothing_length_particle = smoothing_length(particle_system, particle)
    smoothing_length_neighbor = smoothing_length(particle_system, neighbor)

src/schemes/fluid/viscosity.jl:344

  • smoothing_length_neighbor = smoothing_length(particle_system, neighbor) uses the particle system for the neighbor particle. When neighbor_system differs (e.g., boundaries/structures), this will use the wrong smoothing length in the SGS viscosity. Use smoothing_length(neighbor_system, neighbor) here.
    epsilon = viscosity.epsilon

    smoothing_length_particle = smoothing_length(particle_system, particle)
    smoothing_length_neighbor = smoothing_length(particle_system, neighbor)

src/schemes/fluid/viscosity.jl:463

  • smoothing_length_neighbor is computed from particle_system, but neighbor belongs to neighbor_system. This can produce incorrect smoothing_length_average for mixed-system viscosity interactions. Use smoothing_length(neighbor_system, neighbor) instead.
    epsilon = viscosity.epsilon

    smoothing_length_particle = smoothing_length(particle_system, particle)
    smoothing_length_neighbor = smoothing_length(particle_system, neighbor)
    smoothing_length_average = (smoothing_length_particle + smoothing_length_neighbor) / 2

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@efaulhaber efaulhaber marked this pull request as ready for review April 7, 2026 17:23
@efaulhaber efaulhaber requested a review from svchb April 7, 2026 17:23
LasNikas
LasNikas previously approved these changes Apr 9, 2026
@efaulhaber efaulhaber requested a review from LasNikas April 13, 2026 09:15
svchb
svchb previously approved these changes Apr 13, 2026
@svchb svchb enabled auto-merge (squash) April 13, 2026 10:03
@svchb
Copy link
Copy Markdown
Collaborator

svchb commented Apr 13, 2026

/run-gpu-tests

@efaulhaber
Copy link
Copy Markdown
Member Author

/run-gpu-tests

@svchb svchb merged commit 425a7b4 into trixi-framework:main Apr 13, 2026
19 checks passed
@efaulhaber efaulhaber deleted the inplace-viscosity branch April 13, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants